package cn.btks.backstagemanage.role.repository;

import cn.btks.backstagemanage.role.pojo.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author lijun
 * @Date 2021/1/7 11:22
 */
@Repository
public interface RoleRepository extends JpaRepository<Role,Long>, JpaSpecificationExecutor<Role> {

    /**
     * 根据角色编码查询角色信息
     * @param roleCode
     * @return
     */
    Role findByRoleCode(String roleCode);

    /**
     * 根据是否师级查询其下所有角色
     * @param isCity
     * @return
     */
    List<Role> findAllByIsCity(Integer isCity);

    /**
     * 根据角色ID查询是否有角色关联用户信息
     * @param roleId
     * @return
     */
    @Query(value = "select count(*) from user_roles where roles_id = ?1",nativeQuery = true)
    int queryUserRolesCountByRoleId(Long roleId);

    /**
     * 根据角色ID查询是否有角色关联资源清单信息
     * @param roleId
     * @return
     */
    @Query(value = "select count(*) from menu_roles where roles_id = ?1",nativeQuery = true)
    int queryMenuRolesCountByRoleId(Long roleId);

    /**
     * 根据角色ID查询是否有角色关联资源清单信息（加密）
     * @param roleId
     * @return
     */
    @Query(value = "select count(*) from menu_roles where roles_id = ?1",nativeQuery = true)
    int queryMenuRolesCountByRoleId1(String roleId);

    /**
     * 根据资源清单ID查询是否有角色关联资源清单信息
     * @param menuId
     * @return
     */
    @Query(value = "select count(*) from menu_roles where menu_id = ?1",nativeQuery = true)
    int queryMenuRolesCountByMenuId(Long menuId);

    /**
     * 根据资源清单ID及角色ID查询是否有角色关联资源清单信息
     * @param menuId
     * @return
     */
    @Query(value = "select count(*) from menu_roles where menu_id = ?1 and roles_id = ?2",nativeQuery = true)
    int queryMenuRolesCountByMenuIdAndRolesId(Long menuId,Long roleId);

    /**
     * 根据资源清单ID及角色ID查询是否有角色关联资源清单信息（加密）
     * @param menuId
     * @return
     */
    @Query(value = "select count(*) from menu_roles where menu_id = ?1 and roles_id = ?2",nativeQuery = true)
    int queryMenuRolesCountByMenuIdAndRolesId1(String menuId,String roleId);

    /**
     * 根据资源清单ID及角色ID查询是否有角色关联资源清单信息
     * @param menuId
     * @return
     */
    @Query(value = "select menu_id as menuId,roles_id as roleId from menu_roles where menu_id = ?1 and roles_id = ?2",nativeQuery = true)
    Map<String,Object> queryMenuRolesByMenuIdAndRolesId(Long menuId, Long roleId);

    /**
     * 删除角色信息
     * @param id
     * @return
     */
    @Modifying
    @Query(value = "delete from role where id = ?1",nativeQuery = true)
    int deleteRoleById(Long id);

    /**
     * 根据角色ID及资源ID删除关联表数据
     * @param roleId
     * @param menuId
     * @return
     */
    @Modifying
    @Query(value = "delete from menu_roles where roles_id = ?1 and menu_id = ?2",nativeQuery = true)
    int deleteConnectionByRoleIdAndMenuId(Long roleId,Long menuId);
}
